package com.DFB.controller;


import com.DFB.common.model.Result;
import com.DFB.common.model.ResultCode;
import com.DFB.pojo.StaffMerchant;
import com.DFB.service.StaffMerchantService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 员工信息（商铺方） 前端控制器
 * </p>
 *
 * @author DFB
 * @since 2023-09-11
 */
@RestController
@Slf4j
@RequestMapping("/staffMerchant")
public class StaffMerchantController {

    @Autowired
    StaffMerchantService staffMerchantService;

    /**
     * 查询对应商户下所有员工
     *
     * @param storeId
     * @return
     */
    @PostMapping("/staffList")
    public Result<List<StaffMerchant>> getStaffList(@RequestBody String storeId) {
        LambdaQueryWrapper<StaffMerchant> QueryWrapper = new LambdaQueryWrapper<>();
        QueryWrapper.eq(StaffMerchant::getStoreId, storeId);
        List<StaffMerchant> staffMerchantList = staffMerchantService.list(QueryWrapper);
        return Result.success(staffMerchantList);
    }

    /**
     * 添加对应商户下的员工
     *
     * @param staffMerchant
     * @return
     */
    @PostMapping("/addStaff")
    public Result<?> addStaff(@RequestBody StaffMerchant staffMerchant) {
        try {
            staffMerchantService.save(staffMerchant);
            return Result.success(staffMerchant);
        } catch (Exception e) {
            log.info("Error occurred while adding staffMerchant: " + e.getMessage());
            return Result.error(ResultCode.DATA_ALREADY_EXISTED);
        }
    }

    /**
     * 删除员工
     *
     * @param staffId
     * @return
     */
    @PostMapping("/delStaff")
    public Result<?> delStaff(@RequestBody Integer staffId) {
        try {
            staffMerchantService.removeById(staffId);
            return Result.success();
        } catch (Exception e) {
            log.info("Error occurred while delete staffMerchant: " + e.getMessage());
            return Result.error(ResultCode.RESULT_DATA_NONE);
        }
    }

    /**
     * 修改对应商户下的员工信息
     *
     * @param staffMerchant
     * @return
     */
    @PostMapping("/updateStaff")
    public Result<?> updateStaff(@RequestBody StaffMerchant staffMerchant) {
        try {
            staffMerchantService.updateById(staffMerchant);
            return Result.success(staffMerchant);
        } catch (Exception e) {
            log.info("Error occurred while updating staffMerchant: " + e.getMessage());
            return Result.error(ResultCode.DATA_IS_WRONG);
        }
    }

    /**
     * 修改对应商户下员工状态
     *
     * @param staffId
     * @return
     */
    @PostMapping("/staffStatus")
    public Result<?> updateStaffStatus(@RequestBody Integer staffId) {
        try {
            LambdaQueryWrapper<StaffMerchant> wrapper = new LambdaQueryWrapper<>();
            wrapper.select(StaffMerchant::getId, StaffMerchant::getStatus)
                    .eq(StaffMerchant::getId, staffId);
            StaffMerchant staff = staffMerchantService.getOne(wrapper);
            staff.setStatus(staff.getStatus().equals(0) ? 1 : 0);
            staffMerchantService.updateById(staff);
            return Result.success();
        } catch (Exception e) {
            log.info("Error occurred while updating status: " + e.getMessage());
            return Result.error();
        }
    }

}

